package com.example.xijinping.nettest;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xijinping.nettest.Adapter.MyAdapter;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Request;

import static com.example.xijinping.nettest.Json.JsonUtils.parseJson;

/**
 * Created by xijinping on 2016/11/15.
 */
public class ListViewActivity extends AppCompatActivity {

    private ListView lv;
    private ListAdapter adapter    ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.print("sssssss");
        getDatas();
        setContentView(R.layout.activity_list);
    }

    public void getDatas() {
        String url = "http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=1&pageSize=20&v=4.0.0";
        OkHttpUtils
                .get()
                .url(url)
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }

    public class MyStringCallback extends StringCallback {
        @Override
        public void onBefore(Request request, int id) {
            setTitle("loading...");
        }

        @Override
        public void onAfter(int id) {
            setTitle("NetTest");
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            e.printStackTrace();
            System.out.print("aaaaa");
        }

        @Override
        public void onResponse(String response, int id) {
//            mTv.setText("onResponse:" + response);
            System.out.print(response);
            switch (id) {
                case 100:
                    Toast.makeText(ListViewActivity.this, "http", Toast.LENGTH_SHORT).show();
                    break;
                case 101:
                    Toast.makeText(ListViewActivity.this, "https", Toast.LENGTH_SHORT).show();
                    break;
            }
            if (response!=null){
                parseData(response);

            }
        }
    }

    public void parseData(String response) {
        CarBean carBean = parseJson(response);
        ArrayList<CarBean.Car> datas = carBean.getData();
        System.out.print(datas);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new MyAdapter(datas,ListViewActivity.this);
        lv.setAdapter(adapter);
    }
}
